These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
if 10:
print("Hello")
if -10:
print("Hello")
if 0:
print("Hello")
else:
print("Bye")
a=10
b=20
if(a<b):
print("A is Smaller")
else:
print("B is Smaller")
a=int(input("Enter 1st No:"))
b=int(input("Enter 2nd No:"))
if(a<b):
print(a,"is Smaller")
else:
print(b,"is Smaller")
a=10
b=20
if(a<b):
print("Hello")
if(b>a):
print("Hi")
if(a!=b):
print("Bye")
a=10
b=20
if(a<b):
print("Hello")
elif(b>a):
print("Hi")
elif(a!=b):
print("Bye")
else:
print("I am optional")
a=10
b=20
if(a>b):
print("A is greater")
if(a>0):
print("No is +ve")
else:
print("No is -ve")
else:
print("B is greater")
if(b>0):
print("No is +ve")
else:
print("No is -ve")
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
a = 20
b = 10
if a > b: print("a is greater than b")
a = 10
b = 20
print("A is Greater") if a > b else print("B is Greater")
a = 250
b = 30
c = 500
if a > b and c > a:
print("Both conditions are True")
a = 250
b = 30
c = 500
if a > b and a > c:
print("At least one of the conditions is True")
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
a = 10
b = 20
if b > a:
pass
# Write your code here
n=int(input("Enter Number"))
if n>0:
print("No is +Ve")
else:
print("No is -ve")
# Write your code here
n=int(input("Enter Number:"))
if n%2==0:
print("No is even")
else:
print("No is odd")
# Write your code here
n=int(input("Enter Number:"))
if (n & 1)==0:
print("No is even")
else:
print("No is odd")
# Write your code here
n1=int(input("Enter First Number:"))
n2=int(input("Enter Second Number:"))
if n1>n2:
print("First No is Greater",n1)
else:
print("Second No is Greater",n2)
# Write your code here
n1=int(input("Enter First Number:"))
n2=int(input("Enter Second Number:"))
n3=int(input("Enter third Number:"))
if n1>n2 and n1>n3:
print("First No is Greater")
elif n2>n1 and n2>n3:
print("Second no is greater")
else:
print("Third no is greater")
# Write your code here
n=int(input("Enter First Number:"))
if n>6:
if n%2==0:
print("Even")
else:
print("Odd")
else:
if n%3==0:
print("Multiple of 3")
else:
print("Not Multiple of 3")
# Write your code here
P1=0
P2=150
P3=250
n=int(input("Enter Age:"))
if n>=0 and n<=5:
print("Ticket Price:",P1)
elif n>=6 and n<=18:
print("Ticket Price:",P2)
elif n>=19 and n<=24:
print("Ticket Price:",P3)
elif n>=25 and n<=30:
print("Ticket Price:",(P1+P2+P3)*0.75)
elif n>=31 and n<=50:
print("Ticket Price:",((P1+P2+P3)*0.75)*0.97)
else:
print("Go to chaar dhaam yaatra")